package com.java.miaosha.controller;

import com.java.miaosha.constants.Constants;
import com.java.miaosha.entity.MiaoshaOrderEntity;
import com.java.miaosha.entity.OrderEntity;
import com.java.miaosha.entity.UserEntity;
import com.java.miaosha.service.GoodsService;
import com.java.miaosha.service.MiaoshaOrderService;
import com.java.miaosha.service.MiaoshaService;
import com.java.miaosha.service.UserService;
import com.java.miaosha.util.RedisUtils;
import com.java.miaosha.vo.GoodsVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author jiangli
 * @date 2019/8/31 21:23
 */
@Controller
@RequestMapping("/miaosha")
public class MiaoshaController implements InitializingBean{
	@Autowired
	private UserService userService;
	@Autowired
	private GoodsService goodsService;
	@Autowired
	private MiaoshaOrderService miaoshaOrderService;
	@Autowired
	private MiaoshaService miaoshaService;
	@Autowired
	private RedisUtils redisUtils;

	@PostMapping("/do_miaosha")
	public String doMiaosha(Model model, @RequestParam("goodsId")Long goodsId, @CookieValue(value = Constants.TOKEN,required = false)String cookieToken,
							@RequestParam(value = Constants.TOKEN,required = false)String paramToken) {
		if (StringUtils.isEmpty(cookieToken)&&StringUtils.isEmpty(paramToken)){
			//token都为空跳转到登录页
			return "login";
		}
		String token = StringUtils.isNotEmpty(cookieToken)?cookieToken:paramToken;
		UserEntity user = userService.getUserByToken(token);

		GoodsVO goods = goodsService.getGoodsVOById(goodsId);
		//判断库存
		Integer stockCount = goods.getStockCount();
		if (stockCount <= 0) {
			model.addAttribute("errmsg",Constants.GOODS_MIAOSHA_OVER);
			return "miaosha_fail";
		}
		//判断是否重复秒杀,防止一个人秒杀多件商品
		MiaoshaOrderEntity MiaoshaOrder = miaoshaOrderService.getByUserIdAndGoodsId(user.getId(),goods.getId());
		if (MiaoshaOrder != null) {
			model.addAttribute("errmsg",Constants.REPEATE_MIAOSHA);
			return "miaosha_fail";
		}

		//减秒杀库存 下订单 写入秒杀订单
		OrderEntity order = miaoshaService.miaosha(user,goods);
		model.addAttribute("orderInfo",order);
		model.addAttribute("goods",goods);
		return "order_detail";
	}

	/**
	 * 系统初始化
	 * 预先将秒杀商品库存存储redis
	 * @throws Exception
	 */
	@Override
	public void afterPropertiesSet() throws Exception {
		List<GoodsVO> goodsVOS = goodsService.goodsVOList();
		if (!CollectionUtils.isEmpty(goodsVOS)) {
			for (GoodsVO goodsVO : goodsVOS) {
				redisUtils.set(Constants.MIAOSHA_GOODS_STOCK_COUNT+goodsVO.getId(),goodsVO.getStockCount(),7200L);
			}
		}
	}
}
